/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.domain;

import org.hibernate.annotations.Proxy;
import org.xptracker.persistence.Identified;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.Date;

@Entity
@Table(name="story_state")
@Proxy(lazy=false)
public class StoryState extends Identified {
  private String storyId;
  private Date startDate;
  private Date stopDate;
  private String owner;
  private String queue;
  private double estimate;
  private long totalEffort;
  private State state;
  private Severity severity;
  private Size size;
  private Double taskCompletionRatio;

  protected StoryState() {
  }

  public StoryState(Story story) {
    this(new Date(), null, story.getId(), story.getOwner() == null ? null : story.getOwner().getUsername(),
        story.getEstimate(), story.getTotalEffort(), story.getState(), story.getSeverity(), story.getSize(),
        story.getQueue() == null ? null : story.getQueue().getName(), calculateCompletion(story));
  }

  private static double calculateCompletion(Story story) {
    double total = story.getTasks().size();
    double completed = 0;
    for(Task task: story.getTasks()) {
      if (task.isComplete()) {
        completed += 1;
      }
    }
    return total == 0 ? 0 : (completed / total);
  }

  public StoryState(Date startDate, Date stopDate, String storyId, String owner, double estimate, long totalEffort,
                    State state, Severity severity, Size size, String queue, double taskCompletionRatio) {
    this.queue = queue;
    this.startDate = startDate;
    this.stopDate = stopDate;
    this.estimate = estimate;
    this.totalEffort = totalEffort;
    this.size = size;
    this.severity = severity;
    this.state = state;
    this.owner = owner;
    this.storyId = storyId;
    this.taskCompletionRatio = taskCompletionRatio;
  }

  @Column(name="story_id")
  public String getStoryId() {
    return storyId;
  }

  public void setStoryId(String storyId) {
    this.storyId = storyId;
  }

  @Column(name="start_date")
  public Date getStartDate() {
    return startDate;
  }

  public void setStartDate(Date startDate) {
    this.startDate = startDate;
  }

  @Column(name="stop_date")
  public Date getStopDate() {
    return stopDate;
  }

  public void setStopDate(Date stopDate) {
    this.stopDate = stopDate;
  }

  @Column(name="owner")
  public String getOwner() {
    return owner;
  }

  public void setOwner(String owner) {
    this.owner = owner;
  }

  @Column(name="queue")
  public String getQueue() {
    return queue;
  }

  public void setQueue(String queue) {
    this.queue = queue;
  }

  @Column(name="estimate")
  public double getEstimate() {
    return estimate;
  }

  public void setEstimate(double estimate) {
    this.estimate = estimate;
  }

  @Column(name="total_effort")
  public long getTotalEffort() {
    return totalEffort;
  }

  public void setTotalEffort(long totalEffort) {
    this.totalEffort = totalEffort;
  }

  @Column(name="state")
  public State getState() {
    return state;
  }

  public void setState(State state) {
    this.state = state;
  }

  @Column(name="severity")
  public Severity getSeverity() {
    return severity;
  }

  public void setSeverity(Severity severity) {
    this.severity = severity;
  }

  @Column(name="size")
  public Size getSize() {
    return size;
  }

  public void setSize(Size size) {
    this.size = size;
  }

  @Column(name="task_completion")
  public Double getTaskCompletionRatio() {
    return taskCompletionRatio == null ? 0 : taskCompletionRatio;
  }

  public void setTaskCompletionRatio(Double numTasks) {
    this.taskCompletionRatio = numTasks;
  }

  public String toString() {
    return new StringBuilder(storyId).append(", start=").append(startDate).append(", stop=").append(stopDate).
        append(", owner=").append(owner).append(", queue=").append(queue).append(", estimate=").append(estimate).
        append(", effort=").append(totalEffort).append(", state=").append(state).append(", severity=").append(severity).
        append(", size=").append(size).append(", taskCompletion=").append(taskCompletionRatio).toString();
  }
}
